<template>
  <el-dialog v-model="dialogVisible" :title="title" width="30%" :before-close="handleClose">
    <div class="share-dialog">
      <el-form ref="ruleFormRef" label-width="80px" :model="ruleForm" :rules="rules">
        <!--   -->
        <el-form-item label="用户" prop="uid">
          <el-tree-select default-expand-all node-key="id" filterable v-model="ruleForm.uid" :data="treeData"
            check-strictly :render-after-expand="false" :props="{
              label: 'name',
            }" style="width: 100%" placeholder="选择用户" clearable>
          </el-tree-select>
        </el-form-item>

        <el-form-item label="客户类型" v-if="submitType == 'zhuanyi'" prop="own_type" class="form-item-style">
          <el-select v-model="ruleForm.own_type" placeholder="客户类型" style="width: 100%" clearable>
            <el-option label="临时" :value="2" />
            <el-option label="私有" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { assistApi, chgOwnerApi } from "@/plugins/api/customer/manage/my";
import { treePersonApi as listApi } from "@/plugins/api/system/user";

let ruleForm = ref({});
let rules = {
  uid: [
    {
      required: true,
      message: "选择用户",
      trigger: "change",
    },
  ],
  own_type: [
    {
      required: true,
      message: "客户类型",
      trigger: "change",
    },
  ],
};
let dialogVisible = ref(false);
function handleClose(done) {
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => { });
}
let title = ref("");
let submitType = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  ruleForm.value.id = row.id;
  submitType.value = type;
  if (type == "zhuanyi") {
    title.value = "批量转移";
  } else {
    title.value = "批量共享";
  }

  dialogVisible.value = true;
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = Object.assign({}, ruleForm.value);

      if (submitType.value == "zhuanyi") {
        if (userInfo.value.order_number) {
          return ElMessage.warning("请选择人员！");
        }
        chgOwnerApi(obj).then((data) => {
          ElMessage.success("转移成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        assistApi(obj).then((data) => {
          ElMessage.success("共享成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
// 人员信息
let userInfo = computed(() => {
  return findNodeById(treeData.value, ruleForm.value.uid);
});

function findNodeById(tree, id) {
  if (!tree || !tree.length) {
    return null;
  }
  for (const node of tree) {
    if (node.id === id) {
      return node;
    }
    if (node.children && node.children.length) {
      const foundNode = findNodeById(node.children, id);
      if (foundNode) {
        return foundNode;
      }
    }
  }
  return null;
}
//
let treeData = ref([]);
function getUsers() {
  listApi().then((res) => {
    treeData.value = res;
  });
}
onMounted(() => {
  getUsers();
});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss">
.share-dialog {
  .form-item-style {
    width: 100%;
    margin-right: 0;

    :deep(.el-input) {
      width: 100%;
    }
  }
}
</style>
